package com.shopx.core.utils;

import java.io.*;

public class FileUtil {

	public static boolean delFile(String path) {
		File file = new File(path);
		// 路径为文件且不为空则进行删除
		if (file.isFile() && file.exists()) {
			return file.delete();
		}
		return false;
	}

	public static boolean createFile(String path) {
		File file = new File(path);
		// 路径为文件且不为空则进行删除
		if (file.exists()) {
			return true;
		}
		boolean isSuc = file.mkdirs();
		return isSuc;
	}

	public static boolean isFileExists(String path) {
		File file = new File(path);
		if (file.exists()) {
			return true;
		} else {
			return false;
		}
	}

	public static long calFileSize(String filePath) {
		File file = new File(filePath);
		if (file.exists() && file.isFile()) {
			return file.length();
		}
		return 0l;
	}

	public static boolean createFile(String savePath, String fileName, String filecontent) {
		Boolean bool = false;
		String filenameTemp = "";
		filenameTemp = savePath + fileName;// 文件路径+名称+文件类型
		File file = new File(filenameTemp);
		try {
			File dir = file.getParentFile();
			if (!dir.exists()) {
				dir.mkdirs();
			} else {
				file.delete();
				dir.mkdirs();
			}
			file.createNewFile();
			bool = true;
			System.out.println("success create file,the file is " + filenameTemp);
			// 创建文件成功后，写入内容到文件里
			writeFileContent(filenameTemp, filecontent);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return bool;
	}
	
	
	public static boolean writeFileContent(String filepath, String newstr) throws IOException {
		Boolean bool = false;
		String filein = newstr + "\r\n";// 新写入的行，换行
		String temp = "";

		FileInputStream fis = null;
		InputStreamReader isr = null;
		BufferedReader br = null;
		FileOutputStream fos = null;
		PrintWriter pw = null;
		try {
			File file = new File(filepath);// 文件路径(包括文件名称)
			// 将文件读入输入流
			fis = new FileInputStream(file);
			isr = new InputStreamReader(fis);
			br = new BufferedReader(isr);
			StringBuffer buffer = new StringBuffer();

			// 文件原有内容
			for (int i = 0; (temp = br.readLine()) != null; i++) {
				buffer.append(temp);
				// 行与行之间的分隔符 相当于“\n”
				buffer = buffer.append(System.getProperty("line.separator"));
			}
			buffer.append(filein);

			fos = new FileOutputStream(file);
			pw = new PrintWriter(fos);
			pw.write(buffer.toString().toCharArray());
			pw.flush();
			bool = true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			// 不要忘记关闭
			if (pw != null) {
				pw.close();
			}
			if (fos != null) {
				fos.close();
			}
			if (br != null) {
				br.close();
			}
			if (isr != null) {
				isr.close();
			}
			if (fis != null) {
				fis.close();
			}
		}
		return bool;
	}

	/**
	 * 验证文件大小是否超过限制
	 *
	 * @param * @Param null:
	 * @return * @return: null
	 * @author lituo
	 * @date 9:45 AM 2021/10/29
	 */
	public static boolean checkFileSize(Long len, int size, String unit) {
		double fileSize = 0;
		if ("B".equals(unit.toUpperCase())) {
			fileSize = (double) len;
		} else if ("K".equals(unit.toUpperCase())) {
			fileSize = (double) len / 1024;
		} else if ("M".equals(unit.toUpperCase())) {
			fileSize = (double) len / 1048576;
		} else if ("G".equals(unit.toUpperCase())) {
			fileSize = (double) len / 1073741824;
		}
		if (fileSize > size) {
			return false;
		}
		return true;
	}

	public static void main(String[] args) {
		String filePath = "/Users/yons/conf/shopx/shopx-db.properties";
		File file = new File(filePath);
		if (file.exists()) {
			System.out.println("yes");
		} else {
			System.out.println("no");
		}
	}
}
